﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace Volume_Resource_Management
{
    public partial class frmUserManagement : Form
    {
        DateTime infoDate;
        string storeNo;

        public frmUserManagement(string sNo)
        {
            InitializeComponent();
            storeNo = sNo;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Open Schecdule";
            openFileDialog1.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFileDialog1.Filter = "CSV Files (*.csv)|*.csv";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filename = openFileDialog1.FileName;
                StreamReader reader = new StreamReader(filename);
                textBox1.Text = openFileDialog1.FileName;
                string oldName = "";
                string oldDate = "";

                int count = 0;
                
                string import = reader.ReadLine();

                while ((import = reader.ReadLine()) != null)
                {
                    string[] split = import.Split(',');

                    if (split.Length == 29)
                    {
                        string name = split[1].Replace('"', ' ').Trim().ToString() + " " + split[0].Replace('"', ' ').Trim().ToString();
                         
                        if (name != oldName)
                        {
                            if (split[9] != "")
                            {
                                count++;
                                oldDate = split[8];
                                oldName = name;
                            }
                        }
                        else if (name == oldName)
                        {
                            if (split[8] != oldDate)
                            {
                                if (split[9] != "")
                                {
                                    count++;
                                    oldDate = split[8];
                                    oldName = name;
                                }
                            }
                        }
                    }
                }
                
                reader.Close();
                progressBar1.Maximum += count;
                button2.Enabled = true;
                button1.Enabled = false;    
               
            }
        }

        private void frmUserManagement_Load(object sender, EventArgs e)
        {
            generateData();
               
            this.dataGridView1.Columns["ID"].Visible = false;
            this.dataGridView1.Columns["storeNo"].Visible = false;
            this.dataGridView1.Columns["empName"].HeaderText = "Employee Name";
            this.dataGridView1.Columns["workDate"].HeaderText = "Date";
            this.dataGridView1.Columns["startTime"].HeaderText = "Start Time";
            this.dataGridView1.Columns["endTime"].HeaderText = "Finish Time";
            this.dataGridView1.Columns["contHours"].HeaderText = "Contracted Hours";
            this.dataGridView1.Columns[3].Width = 300;

        }

        public void generateData()
        {
            List<employeeInformation> eI = databaseAccessability.getCurrentEmployeeData(storeNo);

            dataGridView1.DataSource = eI;
            progressBar1.Maximum = eI.Count; 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dR = MessageBox.Show("Updating Will Remove All Previous Data" + Environment.NewLine + "Proceed?", "", MessageBoxButtons.YesNo);

            if (dR == System.Windows.Forms.DialogResult.Yes)
            {
                Thread loading = new Thread(update);
                loading.IsBackground = true;
                loading.Start();
            }
        }

        private void update()
        {
            databaseAccessability.truncateEmployees(storeNo, this);

            StreamReader reader = new StreamReader(textBox1.Text);
            textBox1.Text = openFileDialog1.FileName;
            string oldName = "";
            string oldDate = "";

            string import = reader.ReadLine();

            while ((import = reader.ReadLine()) != null)
            {
                string[] split = import.Split(',');

                if (split.Length == 29)
                {
                    string name = split[1].Replace('"', ' ').Trim().ToString() + " " + split[0].Replace('"', ' ').Trim().ToString();

                    if (name != oldName)
                    {
                        if (split[9] != "")
                        {
                            DateTime.TryParse(split[8], out infoDate);

                            databaseAccessability.uploadNewEmployeeInformation(storeNo, infoDate.ToString("dd-MMM-yyyy"), name, split[2], 
                                split[9], split[10], this);

                            oldDate = split[8];
                            oldName = name;
                        }
                    }
                    else if (name == oldName)
                    {
                        if (split[8] != oldDate)
                        {
                            if (split[9] != "")
                            {
                                DateTime.TryParse(split[8], out infoDate);

                                databaseAccessability.uploadNewEmployeeInformation(storeNo, infoDate.ToString("dd-MMM-yyyy"), name, split[2], 
                                    split[9], split[10], this);

                                oldDate = split[8];
                                oldName = name;
                            }
                        }
                    }
                }
            }
            this.Invoke(new Action(() => this.generateData()));
        }
    }
}
